﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using RiskAlert_Entity;
using RiskAlert_Service;
using RiskAlert_Tool;
//using System.Security.Policy;

namespace RiskAlert
{
    internal class Present
    {
        public static void InputToProp<T>(ref T obj, params string[] prop)
        {
            foreach (string s in prop)
            {
                Console.Write($"请输入({s}) >> ");
                foreach (var i in typeof(T).GetProperties())
                {
                    if (i.Name == s)
                    {
                        i.SetValue(obj, Console.ReadLine());
                    }
                }
            }
        }
        public static void ReadPropInObj<T>(T obj)
        {
            foreach (var i in typeof(T).GetProperties())
            {
                object value = i.GetValue(obj);
                if (value == null || value.ToString() == "") { value = "null"; }
                Console.Write($" > {i.Name} [{value}]");
            }
        }
        public static void PrintMenu()
        {
            Console.WriteLine("******  风险警示信息管理系统  ******");
            Console.WriteLine();
            Console.WriteLine("控制选项：\n" +
                                       " 1 >> 增加_指定对象的数据\n" +
                                       " 2 >> 删除_指定对象的evidence数据\n" +
                                       " 3 >> 删除_指定对象及其全部数据\n" +
                                       " 4 >> 读取_指定对象的简要数据\n" +
                                       " 5 >> 读取_指定对象及其全部数据\n" +
                                       " 6 >> 更新_指定对象的简要数据\n" +
                                       " 7 >> 更新_指定对象的evidence数据\n" +
                                       //" 8 >> abcabcabc\n" +
                                       " 0 >> 退出程序");
            Console.Write("请选择：");
        }
        public static void createSeveral(string input)
        {
            string[] every_object = input.Split('|');
            for (int i = 0; i < every_object.Length; i++)
            {
                string creature_str = every_object[i].Remove(every_object[i].IndexOf(';'));
                string evidence_str = every_object[i].Substring(every_object[i].IndexOf(';') + 1);
                //item = new ObjectItem(creature_str.Split(','));
                //evidence = new ObjectItem(evidence_str.Split(','));

            }
        }
        static void Main(string[] args)
        {
            string choice = null;
            string ItemName = null;
            bool isOpened = true;
            string ConnectionStr = "server=127.0.0.1;port=3306;database=study;user=root;password=123456;";
            MySqlConnection connection = new MySqlConnection(ConnectionStr);
            connection.Open();

            while (isOpened)
            {
                InfoService infoService = new InfoService(connection);
                Item item = new Item();
                Item targetItem = null;
                Evidence evidence = new Evidence();
                int count = 0;
                List<Evidence> evidences = new List<Evidence>();

                PrintMenu();
                choice = Console.ReadLine();
                //Console.WriteLine();

                switch (choice)
                {
                    case "1":
                        Console.Write("请输入(ItemName) >> ");
                        ItemName = Console.ReadLine();
                        targetItem = infoService.ReadItem(ItemName);
                        InputToProp(ref evidence, "OccurPos", "EvidenceStatu", "EvidenceDesc", "EvidenceTip");
                        if (targetItem != null)//item若存在 更新item增加evidence
                        {
                            InputToProp(ref item, "ItemDesc", "ItemTip");
                            infoService.OperateItemWithEvidence(item, evidence, true);
                        }
                        else//item若不存在 增加item & evidence
                        {
                            InputToProp(ref item, "ItemPos", "RiskType", "IndustryType", "ItemDesc", "ItemTip");
                            infoService.OperateItemWithEvidence(item, evidence, false);
                        }
                        break;
                    case "2":
                        Console.Write("请输入(ItemName) >> ");
                        ItemName = Console.ReadLine();
                        Console.Write("请输入(count) >> ");
                        count = Convert.ToInt32(Console.ReadLine());
                        infoService.DeleteEvidence(ItemName, count);
                        break;
                    case "3":
                        Console.Write("请输入(ItemName) >> ");
                        infoService.DeleteAll(Console.ReadLine());
                        break;
                    case "4":
                        Console.Write("请输入(ItemName) >> ");
                        item = infoService.ReadItem(Console.ReadLine());
                        if (item != null)
                        {
                            ReadPropInObj(item);
                            Console.ReadKey();
                        }
                        else throw new Exception("指定对象不存在！");
                        break;
                    case "5":
                        Console.Write("请输入(ItemName) >> ");
                        ItemName = Console.ReadLine();
                        item = infoService.ReadItem(ItemName);
                        evidences = infoService.ReadEvidences(ItemName);
                        if (item != null)
                        {
                            ReadPropInObj(item);
                            Console.Write("\n\n");
                            foreach (var i in evidences)
                            {
                                ReadPropInObj(i);
                                Console.WriteLine();
                            }
                            Console.ReadKey();
                        }
                        else throw new Exception("指定对象不存在！");
                        break;
                    case "6":
                        Console.Write("请输入(ItemName) >> ");
                        ItemName = Console.ReadLine();
                        targetItem = infoService.ReadItem(ItemName);
                        if (targetItem != null)
                        {
                            item.ItemId = targetItem.ItemId;
                            InputToProp(ref item, "ItemPos", "RiskType", "IndustryType", "ItemDesc", "ItemTip");
                            infoService.UpdateItem(item);
                        }
                        else throw new Exception("指定对象不存在！");
                        break;
                    case "7":
                        Console.Write("请输入(ItemName) >> ");
                        ItemName = Console.ReadLine();
                        Console.Write("请输入(count) >> ");
                        count = Convert.ToInt32(Console.ReadLine());
                        targetItem = infoService.ReadItem(ItemName);
                        if (targetItem != null)
                        {
                            evidence.EvidenceId = $"{item.ItemId}_{Tool.FormatCount(count)}";
                            //InputToProp("RiskType", ref evidence);//逻辑待完善==========================
                            InputToProp(ref evidence, "OccurPos", "EvidenceStatu", "EvidenceDesc", "EvidenceTip");
                            infoService.UpdateEvidence(evidence);
                        }
                        else throw new Exception("指定对象不存在！");
                        break;
                    case "8":

                        break;
                    case "9":

                        break;
                    case "0":
                        isOpened = false;
                        connection.Close();
                        break;
                    default:
                        Console.WriteLine("选项有误！请重新输入内容！");
                        break;
                }
                Console.WriteLine("操作成功！");
                Thread.Sleep(2000);
                Console.Clear();
            }
            connection.Close();
            Console.WriteLine("程序已退出！");
        }
    }
}
